<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>股票分析系统 - 登录</title>
  <!-- 引入 Bootstrap CSS -->
  <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
  <script src="./js/axios.min.js" type="text/javascript" charset="utf-8"></script>
  <script src="./js/vue.min.js" type="text/javascript" charset="utf-8"></script>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>设置背景图片</title>
  <style>
      body {
          background-image: url('img/4.png'); /* 替换为你实际图片路径 */
          background-size: cover; /* 可选：设置背景图片大小，cover 表示尽可能填充整个容器 */
          background-position: center; /* 可选：设置背景图片位置 */
          /* 可以添加其他背景相关的样式，如颜色、重复等 */
      }
  </style>
</head>
<body>
    <div id="app">
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
      <a class="navbar-brand" href="#">股票分析系统</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav ml-auto">
          <li class="nav-item">
            <a class="nav-link" href="index.html">首页</a>
          </li>
          <li class="nav-item ">
            <a class="nav-link" href="kline1.html">展示页</a>
          </li>
    	  <li class="nav-item">
    	    <a class="nav-link" href="data.html">数据获取</a>
    	  </li>
          <li class="nav-item">
            <a class="nav-link" href="register.html">注册</a>
          </li>
          <li class="nav-item active">
            <a class="nav-link" href="login.html">登录<span class="sr-only">(当前)</span></a>
          </li>
    	  
        </ul>
      </div>
    </nav>

    <div class="container mt-5">
      <div class="row justify-content-center">
        <div class="col-md-6">
          <div class="card">
            <div class="card-body">
              <h5 class="card-title text-center">登录</h5>
              <form @submit.prevent="login">
                <div class="form-group">
                  <label for="username">用户名</label>
                  <input type="text" v-model="userName" class="form-control" id="username" placeholder="请输入用户名" required>
                </div>
                <div class="form-group">
                  <label for="password">密码</label>
                  <input type="password" v-model="password" class="form-control" id="password" placeholder="请输入密码" required>
                </div>
                <button type="submit" class="btn btn-primary btn-block">登录</button>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- 引入 jQuery -->
  <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
  <!-- 引入 Popper.js -->
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"></script>
  <!-- 引入 Bootstrap JS -->
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

  <script type="text/javascript">
    new Vue({
      el: "#app",
      data: {
        userName: "",
        password: ""
      },
      methods: {
        login() {
          let param = new FormData();
          param.append("username", this.userName);
          param.append("password", this.password);
          axios.post("http://127.0.0.1/login", param)
            .then(response => {
              let resultTo = response.data;
              let msg = resultTo.msg;
              if (msg == '登录成功') {
                let userName = resultTo.value.user_username;
                sessionStorage.setItem("username", userName); //放到 sessionStorage   放到浏览器会话
                window.location.href = "./index.html";
              } else if (msg == '登录失败') {
                alert("登录失败");
              }
            })
            .catch(error => {
              console.error("There was an error with the login!", error);
            });
        }
      }
    })
  </script>
</body>
</html>
